//
//  MSRoomHousesListController.m
//  觅尚
//
//  Created by No.8 on 2019/4/25.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSRoomHousesListController.h"
#import "MSRoomHousesModel.h"
#import "MSRoomHousesCell.h"
#define pageSize 10

@interface MSRoomHousesListController ()<UITableViewDelegate,UITableViewDataSource>
//通用导航栏
{
    CustomNavigationBar *naviagtionBar;
    NSInteger page;
    NSInteger count;
}
/** 表格*/
@property(nonatomic,strong)UITableView *tableView;
/** 数组*/
@property(nonatomic,strong)NSMutableArray *roomListArray;


@end

@implementation MSRoomHousesListController

/** Nav实现 */
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    self.view.backgroundColor = backgroundcolor;
    self.roomListArray = [NSMutableArray new];
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
- (void)MJChiBaoZiHeaderRefresh{
    page = 1;
    [self ReloadData];
}
- (void)MJChiBaoZiFootderRefresh{
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    [self CreatView];
    
    [self tableView];
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;

}
- (void)CreatView{
    
    
}
- (void)FootderRefreshReloadData{
    [MBManager showLoading];
    [self.roomListArray removeAllObjects];
    NSDictionary * params = @{@"busid":[WYCAccountTool unarchivebelongBusid],@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSgetRoomList params:params succeed:^(id responseObject) {
        WYCLog(@"==========%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [self.tableView.mj_footer endRefreshing];
            
//            NSMutableArray *temporaryArray = [MSRoomHousesModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
//            [temporaryArray addObjectsFromArray:self.roomListArray];
            self.roomListArray = [MSRoomHousesModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
        
        }else{
            [MBManager hideAlert];
            [self.tableView.mj_footer endRefreshing];
            page = [params[@"page"] integerValue];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)ReloadData{
    [MBManager showLoading];
    [self.roomListArray removeAllObjects];
    NSDictionary * params = @{@"busid":[WYCAccountTool unarchivebelongBusid],@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSgetRoomList params:params succeed:^(id responseObject) {
        WYCLog(@"==========%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [self.tableView.mj_header endRefreshing];
            count = [responseObject[@"data"][@"count"] integerValue];
            NSMutableArray *temporaryArray = [MSRoomHousesModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            [temporaryArray addObjectsFromArray:self.roomListArray];
            self.roomListArray = temporaryArray;
        }else{
            [MBManager hideAlert];
            [self.tableView.mj_header endRefreshing];
            page = [params[@"page"] integerValue];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [self.tableView.mj_header endRefreshing];
    }];
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, ScreenHeight - [self navBarBottom]) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = BackViewColor;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.roomListArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MSRoomHousesCell * cell = [MSRoomHousesCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.RoomHousesModel = self.roomListArray[indexPath.row];
    
    return cell;
}

- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"空闲房间";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MSRoomHousesModel * HousesModel = self.roomListArray[indexPath.row];
//    WYCLog(@"----------%@",HousesModel.roomId);
//    WYCLog(@"----------%@",self.Roomid);
//    NSDictionary * params = @{@"roomId":self.Roomid,@"waiterUid":self.WID,@"openRoomUid":self.RID,@"roomState":@5,@"changeRoomId":HousesModel.roomId};
//    WYCLog(@"更换----------%@",params);
//    [HTTPRequest POST:MSupdateRoomState params:params succeed:^(id responseObject) {
//
//        WYCLog(@"更换----------%@",responseObject);
//        if (CodeStr) {
//            [MBManager showBriefAlert:responseObject[@"msg"]];
            [self.navigationController popViewControllerAnimated:NO];
//        }else{
//            [MBManager showBriefAlert:responseObject[@"msg"]];
//        }
//    } failure:^(NSError *error) {
//        [MBManager showBriefAlert:Failed];
//    }];
    
    
    if (self.replaceButtonDelegate && [self.replaceButtonDelegate respondsToSelector:@selector(ClickreplaceButtonRoomID:RoomName:)]) {
        [self.replaceButtonDelegate ClickreplaceButtonRoomID:HousesModel.roomId RoomName:HousesModel.roomName];
    }
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
